package com.yidu.qianfeng.service.impl;

import com.yidu.qianfeng.common.bean.company.EmpInfo;
import com.yidu.qianfeng.common.bean.finance.Finance;
import com.yidu.qianfeng.common.bean.finance.Income;
import com.yidu.qianfeng.common.bean.finance.Spend;
import com.yidu.qianfeng.mapper.FinanceMapper;
import com.yidu.qianfeng.mapper.IncomeMapper;
import com.yidu.qianfeng.mapper.SpendMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: TangSheng
 * @Description: 财务记录api
 * @Date Created in 14:47 2021/4/22
 * @since 1.0
 */
@Service("financeApi")
public class FinanceApi {

    @Resource(name = "financeMapper")
    private FinanceMapper financeMapper;

    @Resource(name = "incomeMapper")
    private IncomeMapper incomeMapper;

    @Resource(name = "spendMapper")
    private SpendMapper spendMapper;

    /**
     *  记录财务
     * @param isIncome 是否是收入，参数为：1时为收入，否则都为支出
     * @param course 收支科目
     * @param money 收支金额
     * @param content 收支内容
     * @param consignNum 收支凭证运单号
     * @param payObj 收支对象公司
     * @param arrivalStation 收支发生站点
     * @return 结果
     */
    public int financeRecord(int isIncome, String course, double money, String content, String consignNum, String payObj, String arrivalStation,EmpInfo loginEmp){
        int count = 0;

        if (isIncome == 1){
            Income income = new Income();
            //收入科目
            income.setIncomeCourse(course);
            //收入金额
            income.setIncomeCredit(money);
            //收入内容
            income.setIncomeContent(content);
            //收入凭证号
            income.setIncomeVoucherNum(consignNum);
            //收入付款对象
            income.setIncomePayObj(payObj);
            //进港站点
            income.setIncomeArrivalStation(arrivalStation);

            count += generateFinance(income, null, loginEmp);
        }else {
            Spend spend = new Spend();
            //收入科目
            spend.setSpendCourse(course);
            //收入金额
            spend.setSpendDebit(money);
            //收入内容
            spend.setSpendContent(content);
            //收入凭证号
            spend.setSpendVoucherNum(consignNum);
            //收入付款对象
            spend.setSpendPayObj(payObj);
            //进港站点
            spend.setSpendArrivalStation(arrivalStation);

            count += generateFinance(null, spend,loginEmp);
        }

        return count;
    }

    @Transactional
    public int generateFinance(Income income,Spend spend,EmpInfo loginEmp){
        int count = 0;

        Finance finance = new Finance();
        finance.setInspendId(UUID.randomUUID().toString());

        if (income != null){
            Income incomeEntity = generateIncomeEntity(income, loginEmp);
            //资金项目
            finance.setInspendFound("收入");
            //录入日期
            finance.setInspendDate(incomeEntity.getIncomeEnteringDate());
            //支出金额
            finance.setInspendSpendmoney(null);
            //收入金额
            finance.setInspendIncomemoney(incomeEntity.getIncomeCredit());
            //收支科目
            finance.setInspendCourse(incomeEntity.getIncomeCourse());
            //收支类型
            finance.setInspendType(incomeEntity.getIncomeType());
            //进港站点
            finance.setInspendArrivalStation(incomeEntity.getIncomeArrivalStation());
            //发生站点
            finance.setInspendOccurStation(incomeEntity.getIncomeSubstId());
            //收支方式
            finance.setInspendWay(incomeEntity.getIncomeWay());
            //收支内容
            finance.setInspendContent(incomeEntity.getIncomeContent());
            //收支操作员
            finance.setInspendOperator(incomeEntity.getIncomeTypeinPerson());
            //付款对象
            finance.setInspendPayObj(incomeEntity.getIncomePayObj());
            //收支备注
            finance.setInspendRemark(incomeEntity.getIncomeRemark());
            //收支个人审核
            finance.setInspendPersonCheck("否");
            //收支审核人
            finance.setInspendVerifier("");
            //收支分站审核
            finance.setInspendSubstationCheck("否");
            //收支分站审核人
            finance.setInspendSubstationVerifier("");
            //凭证号
            finance.setInspendVoucherNum(incomeEntity.getIncomeVoucherNum());

            count += incomeMapper.insert(incomeEntity);
            count += financeMapper.insert(finance);

        }
        if (spend != null){
            Spend spendEntity = generateSpendEntity(spend,loginEmp);

            //资金项目
            finance.setInspendFound("开支");
            //录入日期
            finance.setInspendDate(spendEntity.getSpendEnteringDate());
            //支出金额
            finance.setInspendSpendmoney(spendEntity.getSpendDebit());
            //收入金额
            finance.setInspendIncomemoney(null);
            //收支科目
            finance.setInspendCourse(spendEntity.getSpendCourse());
            //收支类型
            finance.setInspendType(spendEntity.getSpendType());
            //进港站点
            finance.setInspendArrivalStation(spendEntity.getSpendArrivalStation());
            //发生站点
            finance.setInspendOccurStation(spendEntity.getSpendSubstId());
            //收支方式
            finance.setInspendWay(spendEntity.getSpendWay());
            //收支内容
            finance.setInspendContent(spendEntity.getSpendContent());
            //收支操作员
            finance.setInspendOperator(spendEntity.getSpendTypeinPerson());
            //付款对象
            finance.setInspendPayObj(spendEntity.getSpendPayObj());
            //收支备注
            finance.setInspendRemark(spendEntity.getSpendRemark());
            //收支个人审核
            finance.setInspendPersonCheck("否");
            //收支审核人
            finance.setInspendVerifier("");
            //收支分站审核
            finance.setInspendSubstationCheck("否");
            //收支分站审核人
            finance.setInspendSubstationVerifier("");
            //凭证号
            finance.setInspendVoucherNum(spendEntity.getSpendVoucherNum());

            count += spendMapper.insert(spendEntity);
            count += financeMapper.insert(finance);
        }

        return count;
    }



    public Income generateIncomeEntity(Income income, EmpInfo loginEmp){
        income.setIncomeId(UUID.randomUUID().toString());
        //录入日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        income.setIncomeEnteringDate(formatter.format(new Date()));
        //操作站点
        income.setIncomeSubstId(loginEmp.getEmpSubstId());
        //收入类型
        income.setIncomeType("出港");
        //录入员
        income.setIncomeTypeinPerson(loginEmp.getEmpName());
        //收支方式
        income.setIncomeWay("现金");

        /*//收入科目
        income.setIncomeCourse("");
        //收入金额
        income.setIncomeCredit(123.1);
        //收入内容
        income.setIncomeContent("~~~~~~收入内容");
        //收入凭证号
        income.setIncomeVoucherNum("托运运单号");
        //收入付款对象
        income.setIncomePayObj("此处添发货方单位");
        //进港站点
        income.setIncomeArrivalStation("此处添出发站所在站点");*/

        return income;
    }

    public Spend generateSpendEntity(Spend spend, EmpInfo loginEmp){
        spend.setSpendId(UUID.randomUUID().toString());
        //录入日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        spend.setSpendEnteringDate(formatter.format(new Date()));
        //操作站点
        spend.setSpendSubstId(loginEmp.getEmpSubstId());
        //开支类型
        spend.setSpendType("出港");
        //录入员
        spend.setSpendTypeinPerson(loginEmp.getEmpName());
        //收支方式
        spend.setSpendWay("现金");


        /*//开支科目
        spend.setSpendCourse("");
        //开支金额
        spend.setSpendDebit(123.1);
        //开支内容
        spend.setSpendContent("~~~~~~收入内容");
        //开支凭证号
        spend.setSpendVoucherNum("托运运单号");
        //开支付款对象
        spend.setSpendPayObj("此处添发货方单位");
        //进港站点
        spend.setSpendArrivalStation("此处添出发站所在站点");*/


        return spend;
    }


}
